package com.heima.wemedia.controller.v1;

import com.heima.common.exception.LeadNewsException;
import com.heima.model.common.dto.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dto.NewsAuthDto;
import com.heima.model.wemedia.dto.WmNewsDto;
import com.heima.model.wemedia.dto.WmNewsPageReqDto;
import com.heima.model.wemedia.pojo.WmNews;
import com.heima.wemedia.service.NewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.ws.rs.POST;
import java.util.Objects;

/**
 * @author Ronaldo
 * @date 2021/8/4
 */
@RestController
@RequestMapping("/api/v1/news")
@Slf4j
public class NewsController {

    @Autowired
    private NewsService newsService;

    @PostMapping("/list")
    public ResponseResult queryNewsListByPage(@RequestBody WmNewsPageReqDto newsPageReqDto){

        log.info("page query news list by newsPageDTO{}" + newsPageReqDto);

        if (Objects.isNull(newsPageReqDto)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return newsService.queryNewsListByPage(newsPageReqDto);
    }

    @PostMapping("/submit")
    public ResponseResult submitOrSave(@RequestBody WmNewsDto newsDto){

        log.info("submit for review or save draft by WmNewsDTO{}" + newsDto );

        if (Objects.isNull(newsDto)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return newsService.submit(newsDto);
    }

    @GetMapping("/one/{id}")
    public ResponseResult getNewsById(@PathVariable("id") Integer id){

        log.info("get news by id{}" + id);

        if (Objects.isNull(id)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return newsService.getNewsById(id);
    }

    @GetMapping("/del_news/{id}")
    public ResponseResult deleteNewsById(@PathVariable("id") Integer id){

        log.info("delete news by id{}" + id);

        if (Objects.isNull(id)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return newsService.deleteNewsById(id);
    }

    @PostMapping("/down_or_up")
    public ResponseResult downOrUp(@RequestBody WmNewsDto newsDto) {

        if (Objects.isNull(newsDto)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return newsService.downOrUp(newsDto);
    }

    @PostMapping("/list_vo")
    public ResponseResult getNewsListForAdmin(@RequestBody NewsAuthDto newsAuthDto){
        log.info("get news list for admin scan by newsAuthDTO{}",newsAuthDto);

        if (Objects.isNull(newsAuthDto)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        newsAuthDto.checkParam();
        return newsService.getNewsListForAdmin(newsAuthDto);
    }

    @GetMapping("/one_vo/{id}")
    public ResponseResult getNewsForAdminById(@PathVariable Integer id){
        log.info("get news for admin scan by newsId{}",id);

        if (Objects.isNull(id)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return newsService.getNewsForAdminById(id);
    }

    @PostMapping("/auth_pass")
    public ResponseResult authPass(@RequestBody NewsAuthDto newsAuthDto){
        log.info("admin Manual scan{}",newsAuthDto);
        if (Objects.isNull(newsAuthDto)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return newsService.manualScan(newsAuthDto,WmNews.Status.ADMIN_SUCCESS.getCode());
    }

    @PostMapping("/auth_fail")
    public ResponseResult authFail(@RequestBody NewsAuthDto newsAuthDto){

        log.info("admin Manual scan{}",newsAuthDto);

        if (Objects.isNull(newsAuthDto)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return newsService.manualScan(newsAuthDto,WmNews.Status.FAIL.getCode());
    }

}
